/*
 * lark firmware - used by the lark wireless device
 *
 * PHY and RF functions
 *
 * Copyright (c) 2000-2014 CJTian <cjtian@actions-semi.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

void set_phy_reg(u8 ra, u32 rd)
{
	while(get(MAC_PHY_REG_ACCESS_CON) & BIT(0) == BIT(0));
	set(MAC_PHY_REG_RW_DATA, cpu_to_le32(rd));
    set(MAC_PHY_REG_ACCESS_CON, cpu_to_le32(ra << 2| BIT(0)));
}

void read_phy_reg(u8 ra, u32 *rd)
{
	while(get(MAC_PHY_REG_ACCESS_CON) & BIT(0) == BIT(0));
    set(MAC_PHY_REG_ACCESS_CON, cpu_to_le32(ra << 2 | 0x3));
	while(get(MAC_PHY_REG_ACCESS_CON) & BIT(0) == BIT(0));
    *rd = get(MAC_PHY_REG_RW_DATA);
}

void set_rf_reg(u8 address, u32 data) 
{
    u32 spi_data;

    spi_data = ((data << 4) + address) << (32 - RF_SPI_DATA_WIDTH);

    set_phy_reg(SPIDATA, spi_data);

    spi_data = (RF_SPI_DATA_WIDTH << 2) | BIT(0);
    set_phy_reg(SPICTRL, spi_data);

    do {
        read_phy_reg(SPICTRL, &spi_data);
    } while(spi_data & BIT(0));
}

static uint32_t rf_init(const bool initialize)
{
	return 0;
}
